// JavaScript Document

function removeRow(index){
		var jml=0;
   	 	$('#row_brg_'+modul+'_'+idx).remove();
		$( ".tot_na" ).each(function( index ) {
			jml = jml+parseInt($(this).html());
		   
		});
		$('#grand_total').html(jml);
		$('#nilai_faktur_'+modul).html(jml);
		$('#tot_nilai_faktur').val(jml);
		
}	
		
	$('.angka').numberbox({
		min:0,
	});
	
function simpan_ah(){
	loadingna();
	submit_form(modul+'_form',function(r){
		console.log(r);
		if(r==1){
			if(modul=='penerimaan'){
				$.messager.alert('Save Data','Data Sudah Tersimpan Dan Sudah MAsuk Ke Stock','info');
			}else{
				$.messager.alert('Save Data','Data Sudah Tersimpan','info');
			}
			$('#'+modul+'_'+modul_main+'_grid').datagrid('reload');
			$('#isi_na_'+modul).css('display','inline');
			$('#form_naa_'+modul).css('display','none');
		}		
		else{//$.messager.alert('Save Data','Gagal Simpan Cek Atribut Penginputan','error');
			$.messager.alert('Save Data',r,'error');
		}
		winLoadingClose();
	});
}
$('#btnBatal_'+modul).bind('click',function(){
	$('#isi_na_'+modul).css('display','inline');
	$('#form_naa_'+modul).css('display','none');
});
//$('#btnSend_'+modul).die();
$('#btnSend_'+modul).bind('click',function(){
	if ($('#'+modul+'_form').form('validate')){
		simpan_ah();
	}
	else{
		$.messager.alert('Input Data','ISI DATA YANG KOSONG!!!', 'Info');
	}
	
});
$('#btnHit_'+modul).die();
$('#btnHit_'+modul).live('click',function(){
	var sub_tot=($('#tot_nilai_faktur').val()-$('#potongan').val());
	var nil_ppn=(sub_tot*10)/100;
	var jml_ppn=(sub_tot+nil_ppn);
	$('#sub_tot_'+modul).html(sub_tot);
	$('#ppn_'+modul).html(nil_ppn);
	$('#tot_ppn'+modul).html(jml_ppn);
});
$('#get_karyawan_'+modul).on('click', function(){
	$.post('procurement/generaldisplay/searchwindow',{'type':modul, 'type_window':'karyawan_proc'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});

$('#get_no_pemesanan_'+modul).on('click', function(){
	loadingna();
	$('#table_na').html('').addClass('loading');
	$.post('procurement/datapendukung/no_pemesanan_supplier',function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
		winLoadingClose();
	});
});
$('#get_no_faktur_'+modul).bind('click', function(){
	loadingna();
	$('#data_barangna').html('').addClass('loading');
	$.post('procurement/datapendukung/no_faktur',{modul:modul},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
		winLoadingClose();
	});
});
$('#get_supplier_'+modul).on('click', function(){
	$.post('home/generaldisplay/searchwindow',{'type':modul, 'type_window':'supplier_proc'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});
/*
$('#get_no_faktur_'+modul).on('click', function(){
	$.post('home/generaldisplay/searchwindow',{'type':modul, 'type_window':'faktur_proc'},function(resp){
		windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
	});
});
*/
function get_barang(nomernya){
		$.post('home/generaldisplay/searchwindow',{'type':modul, 'type_window':'barang_proc', nomer:nomernya},function(resp){
			windowForm2(resp, 'Search Data', frmWidth-820, frmHeight-260);
		});
	}	
	
function hitung_na(no){
		var qty=$('#qty_'+modul+'_'+no).val();
		var harga=parseInt($('#harga_na_'+modul+'_'+no).html());
		var ppn=parseInt($('#ppn_na_'+modul+'_'+no).html());
		var hasil;
		var harga_ppn;
		var jml=0;
		//console.log(qty);
		hasil=qty*harga;
		harga_ppn=hasil+ppn;
		$('#tot_harga_na_'+modul+'_'+no).html(NumberFormat(hasil));	
		$('#harga_ppn_'+modul+'_'+no).html(NumberFormat(harga_ppn));	
		$('#harga_ppn_'+modul+'_'+no).attr('val',harga_ppn);	
		if(modul=='pemesanan_cabang'){
			$( ".angka" ).each(function( index ) {
				jml = jml+parseInt($(this).val());
		   
			});
			$('#grand_total_'+modul).html(jml);
		}
		else{
			$( ".tot_na_"+modul ).each(function( index ) {
				var a
				if($(this).attr('val')==''){a=0;}
				else{a=$(this).attr('val');}
				jml = jml+parseInt(a);
			   
			});
			
			
			$('#grand_total_'+modul).html(NumberFormat(jml));
			$('#nilai_faktur_'+modul).html(jml);
			$('#tot_nilai_faktur').val(jml);
		}
	}


if(modul=='pemesanan_cabang'){
	$('.add_row').die();
	$('.add_row').live('click',function(){
		idx++;
		var row_na =' <tr id="row_brg_'+modul+'_'+idx+'"><td align="center"><input type="text" id="kd_brg_'+modul+'_'+idx+'" style="width:200px;" /><a id="get_dtbarang_'+modul+'_'+idx+'" onClick="get_barang('+idx+');" class="l-btn l-btn-plain" href="#"><span class="l-btn-left"><span class="l-btn-text icon-search" style="padding-left: 20px;">&nbsp;</span></span></a><input type="hidden" id="kode_barang_'+modul+'_'+idx+'" name="id_barang[]">&nbsp;</td><td><span id="merk_na_'+modul+'_'+idx+'"></span></td><td align="center"><input type="text" name="qty[]"  style="width:50px;" class="angka" id="qty_'+modul+'_'+idx+'"  onkeyup="hitung_na('+idx+')" /></td><td align="center"><span id="satuan_na_'+modul+'_'+idx+'"></span></td><td align="center"><a href="#"  class="add_row" title="Tambah"><img src="'+host+'assets/css/themes_easyui/icons/add.png" /></a>&nbsp;<a href="#"  class="hapus_row" onclick="removeRow('+idx+')" title="Hapus"><img src="'+host+'assets/css/themes_easyui/icons/delete.png" /></a><input type="hidden" name="id_detil[]" value="" ><input type="hidden" name="sts_detil[]" value="add" ></td></tr>';
		//var row_na = '<tr id="row_brg_'+idx+'"> ';
			
		$('#table-2').append(row_na)
		$('.angka').numberbox({
			min:0,
		});
	});
	
	if(sts=='edit'){hitung_na();}
}

if(modul=='pemesanan'){
	$('#cari_karyawan').die();
	$('#cari_karyawan').live('click',function(){
		//alert($('#id_karyawan').val());
		loadingna();
		$.post(host+'procurement/get_data/data_karyawan',{nik:$('#id_karyawan').val()},function(r){
			var data=JSON.parse(r);
			//console.log(data);
			if(data.msg==0){
				$.messager.alert('Data Karyawan','NIK Karyawan Tidak Sesuai','Data');	
				$('#nama_karyawan').html('-');
				$('#jabatan').html('-');
				$('#departemen').html('-');
				$('#divisi').html('-');
				$('#nama_dealer').html('-');
				$('#group_dealer').html('-');
				$('#id_karyawan').focus();
			}
			else{
				//$.messager.alert('Data Karyawan','NIK Karyawan OK','Data');
				$('#nama_karyawan').html(data.nama);
				$('#jabatan').html(data.jabatan);
				$('#departemen').html(data.departemen);
				$('#divisi').html(data.divisi);
				$('#nama_dealer').html(data.dealer);
				$('#group_dealer').html(data.group_dealer);
			}
			winLoadingClose();
		});
	});
	
	$('#cari_supplier').die();
	$('#cari_supplier').live('click',function(){
		//alert($('#id_karyawan').val());
		loadingna();
		$.post(host+'procurement/get_data/data_supplier',{kd_supplier:$('#kode_supplier').val()},function(r){
			var data=JSON.parse(r);
			//console.log(data);
			if(data.msg==0){
				$.messager.alert('Data Supplier','Kode Supplier Tidak Sesuai','Data');	
				$('#nama_supplier').html('-');
				$('#kota').html('-');
				$('#alamat').html('-');
				$('#telp').html('-');
				$('#email').html('-');
				$('#kode_supplier').focus();
			}
			else{
				//$.messager.alert('Data Karyawan','NIK Karyawan OK','Data');
				$('#nama_supplier').html(data.nama);
				$('#kota').html(data.kota);
				$('#alamat').html(data.alamat);
				$('#telp').html(data.telp);
				$('#email').html(data.email);
				
			}
			winLoadingClose();
		});
	});
	/*
	$('#kode_supplier').keydown(function(e) {	
		if(e.keyCode==13){
		  	$('#cari_supplier').trigger('click');
		}
	});
	
	$('#id_karyawan').keydown(function(e) {	
		if(e.keyCode==13){
		  	$('#cari_karyawan').trigger('click');
		}
	});
	*/	
	$('.add_row').die();
	$('.add_row').live('click',function(){
		idx++;
		var row_na =' <tr id="row_brg_'+modul+'_'+idx+'"><td align="center"><input type="text" id="kd_brg_'+modul+'_'+idx+'" /><a id="get_dtbarang_'+modul+'_'+idx+'" onClick="get_barang('+idx+');" class="l-btn l-btn-plain" href="#"><span class="l-btn-left"><span class="l-btn-text icon-search" style="padding-left: 20px;">&nbsp;</span></span></a><input type="hidden" id="kode_barang_'+modul+'_'+idx+'" name="id_barang[]">&nbsp;</td><td><span id="group_na_'+modul+'_'+idx+'"></span></td><td><span id="kategori_na_'+modul+'_'+idx+'"></span></td><td><span id="nama_na_'+modul+'_'+idx+'"></span></td><td><span id="merk_na_'+modul+'_'+idx+'"></span></td><td align="center"><input type="text" name="qty[]"  style="width:30px;" class="angka" id="qty_'+modul+'_'+idx+'"  onkeyup="hitung_na('+idx+')" /></td><td><span id="satuan_na_'+modul+'_'+idx+'"></span></td><td align="right"><span id="harga_na_'+modul+'_'+idx+'"></span></td><td align="right"><span id="tot_harga_na_'+modul+'_'+idx+'"></span></td><td align="right"><span id="dpp_na_'+modul+'_'+idx+'"></span></td><td align="right"><span id="ppn_na_'+modul+'_'+idx+'"></span></td><td align="right"><span id="harga_ppn_'+modul+'_'+idx+'"></span></td><td align="center"><a href="#"  class="add_row" title="Tambah"><img src="'+host+'assets/css/themes_easyui/icons/add.png" /></a>&nbsp;<a href="#"  class="hapus_row" onclick="removeRow('+idx+')" title="Hapus"><img src="'+host+'assets/css/themes_easyui/icons/delete.png" /></a><input type="hidden" name="id_detil[]" value="" ><input type="hidden" name="sts_detil[]" value="add" ></td></tr>';
		//var row_na = '<tr id="row_brg_'+idx+'"> ';
			
		$('#table-2').append(row_na)
		$('.angka').numberbox({
			min:0,
		});
	});
		
	if(sts=='edit'){hitung_na();}
	
}

if(modul=='pembelian'){
	$('#termin_1').datebox({formatter: formatDate });
	$('#termin_2').datebox({formatter: formatDate });
	$('#termin_3').datebox({formatter: formatDate });
	$('#termin_1').datebox('setValue',today);
	$('#termin_2').datebox('setValue',today);
	$('#termin_3').datebox('setValue',today);
	if(sts=='edit'){$('#btnHit_'+modul).trigger('click');}
	$('.add_row').die();
	$('.add_row').live('click',function(){
		idx++;
		var row_na =' <tr id="row_brg_'+modul+'_'+idx+'"><td align="center"><input type="text" id="kd_brg_'+modul+'_'+idx+'" /><a id="get_dtbarang_'+modul+'_'+idx+'" onClick="get_barang('+idx+');" class="l-btn l-btn-plain" href="#"><span class="l-btn-left"><span class="l-btn-text icon-search" style="padding-left: 20px;">&nbsp;</span></span></a><input type="hidden" id="kode_barang_'+modul+'_'+idx+'" name="id_barang[]">&nbsp;</td><td><span id="kategori_na_'+modul+'_'+idx+'"></span></td><td align="center"><input type="text" name="qty[]"  style="width:30px;" class="angka" id="qty_'+modul+'_'+idx+'"  onkeyup="hitung_na('+idx+')" /></td><td><span id="satuan_na_'+modul+'_'+idx+'"></span></td><td align="right"><span id="harga_na_'+modul+'_'+idx+'"></span></td><td align="right"><span id="tot_harga_na_'+modul+'_'+idx+'"  class="tot_na"></span></td><td align="center"><a href="#"  class="add_row" title="Tambah"><img src="'+host+'assets/css/themes_easyui/icons/add.png" /></a>&nbsp;<a href="#"  class="hapus_row" onclick="removeRow('+idx+')" title="Hapus"><img src="'+host+'assets/css/themes_easyui/icons/delete.png" /></a><input type="hidden" name="id_detil[]" value="" ><input type="hidden" name="sts_detil[]" value="add" ></td></tr>';
		//var row_na = '<tr id="row_brg_'+idx+'"> ';
			
		$('#table-2').append(row_na)
		$('.angka').numberbox({
			min:0,
		});
	});
	
}
if(modul=='penerimaan'){
	$('#tgl_surat_jalan').datebox({formatter: formatDate });
	$('#tgl_surat_jalan').datebox('setValue',today);
	if(sts=='edit'){
		$('#data_barangna').html('').addClass('loading');
		$.post(host+'procurement/get_table/detil_penerimaan',{'id':$('#tbl_faktur_pembelian_id_'+modul).val(),'modul':modul,sts:'edit'},function(r){
			$('#data_barangna').removeClass('loading').html(r);
			$('.angka').numberbox({
				min:0,
			});
			hitung_na();
		});	
	}
	$('.angka').numberbox({
			min:0,
	});
}
if(modul=='retur'){
	//$('#tbl_faktur_pembelian_id_'+modul)
	$('#data_barangna_'+modul).html('').addClass('loading');
		
}

function cek_sisa(idx){
	
	
	
	$( ".cek" ).each(function( index ) {
		var sisa_na = parseInt($('#qty_sisa_'+index).val());
		var input_na = parseInt($(this).val());
		if(input_na > sisa_na){
			$.messager.alert('Tidak Valid','Qty Tidak Boleh Melebihi Sisa','error');
			$('#btnSend_'+modul).hide();
			return false;
		}
		else{
			$('#btnSend_'+modul).show();
		}
		
	});
	
	
	
	console.log(input_na+' = '+sisa_na);
}
